package com.panshi.hujin2.httpcrawler.pdf;

import com.panshi.hujin2.httpcrawler.domain.bank.CreditCardBillDo;
import com.panshi.hujin2.httpcrawler.enumtype.BankTypeEnum;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.File;
import java.io.IOException;

/**
 * @Author: wangxl
 * @Date: 2019/1/16 19:05
 */
public class BillPdfParseUtil {

    private static Logger logger = LoggerFactory.getLogger(BillPdfParseUtil.class);

    /**
     * 解析PDF
     *
     * @param fileName
     * @return
     */
    public static CreditCardBillDo parsePdf(String fileName, int bankType) throws IOException {

        CreditCardBillDo creditCardBillDo = null;
        if (bankType == BankTypeEnum.BRADESCO.getType()) {
            creditCardBillDo = BradescoPdfUtil.parsePdfFile(fileName);
        } else if (bankType == BankTypeEnum.SANTANDER.getType()) {
            creditCardBillDo = SantanderPdfUtil.parsePdfFile(fileName);
        } else if (bankType == BankTypeEnum.CAIXA.getType()) {
            // 此银行PDF账单需要输入密码（cpf）
            creditCardBillDo = CaixaPdfUtil.parsePdfFile(fileName, "");
        } else if (bankType == BankTypeEnum.ITAU.getType()) {
            creditCardBillDo = ItauPdfUtil.parsePdfFile(fileName);
        } else if (bankType == BankTypeEnum.NUBANK.getType()) {
            creditCardBillDo = NuBankPdfUtil.parsePdfFile(fileName);
        } else {
            throw new RuntimeException("there is no matched pdf util");
        }

        //删除已下载的pdf
        File billPdf = new File(fileName);
        if (billPdf.exists()) {
            boolean delete = billPdf.delete();
            if (delete) {
                logger.info("delete file success");
            } else {
                logger.info("delete file fail");
            }
        }
        return creditCardBillDo;
    }
}
